//
//  EntertainmentViewController.m
//  噗嗤
//
//  Created by 火火火火火 on 16/8/16.
//  Copyright © 2016年 cjp. All rights reserved.
//

#define LeeWidth [UIScreen mainScreen].bounds.size.width
#define LeeHeight self.view.frame.size.height

#import "EntertainmentViewController.h"
#import "JokeViewController.h"
#import "FunnyPicViewController.h"
#import "BeautyViewController.h"

static CGFloat const buttonY = 0,buttonH = 44;

@interface EntertainmentViewController ()<UIScrollViewDelegate>

//头部标题的ScrollView
@property(nonatomic,strong) UIScrollView *titleScView;
//页面滚动的ScrollView
@property(nonatomic,strong) UIScrollView *pagesScVIew;

@property(nonatomic,weak) UIButton *selButton;
//存放标题的数组
@property(nonatomic,strong)NSMutableArray *titleButtons;

@property (assign , nonatomic) NSInteger buttonW;

@end

@implementation EntertainmentViewController

-(NSMutableArray *)titleButtons{
    if(!_titleButtons){
        _titleButtons = [NSMutableArray array];
    }
    return _titleButtons;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.buttonW = LeeWidth / 3;
    
    self.titleScView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64,LeeWidth,44)];
    self.titleScView.backgroundColor = [UIColor whiteColor];
    self.pagesScVIew = [[UIScrollView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(self.titleScView.frame), LeeWidth, LeeHeight - CGRectGetMaxY(self.titleScView.frame))];
    
    [self.view addSubview:self.titleScView];
    [self.view addSubview:self.pagesScVIew];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    self.pagesScVIew.delegate = self;
    
    
    [self addAllChildViewControllers];
    [self addChildViewButton];
    [self setHeadSV];
    [self setPagesSV];
    
}

//头部标题的ScrollView
-(void)setHeadSV{
    NSUInteger count = self.childViewControllers.count;
    self.titleScView.contentSize = CGSizeMake(count * self.buttonW,0);
    self.titleScView.showsHorizontalScrollIndicator = NO;

}

//页面滚动的ScrollView
-(void)setPagesSV{
    
    self.pagesScVIew.contentSize = CGSizeMake(LeeWidth * self.childViewControllers.count, 0);
    self.pagesScVIew.showsHorizontalScrollIndicator = NO;
    self.pagesScVIew.pagingEnabled = YES;
    
}

//添加所有子视图控制器
-(void)addAllChildViewControllers{
    
//  段子
    JokeViewController *joke = [JokeViewController new];
    joke.title = @"段子";
    [self addChildViewController:joke];
//    趣图
    FunnyPicViewController *funny = [FunnyPicViewController new];
    funny.title = @"趣图";
    [self addChildViewController:funny];
    //美女
    BeautyViewController * beauty = [BeautyViewController new];
    beauty.title = @"美女";
    [self addChildViewController:beauty];
    
}


//添加子视图控制器对应标题
-(void)addChildViewButton{
    
    NSUInteger count = self.childViewControllers.count;
    
    for(int i = 0; i < count; i++){
        
        //获取子视图控制器
        UIViewController *vc = self.childViewControllers[i];
        
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        button.frame = CGRectMake(self.buttonW * i,buttonY, self.buttonW, buttonH);
        [button setTitle:vc.title forState:(UIControlStateNormal)];
        [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [button setTitleColor:[UIColor redColor] forState:(UIControlStateSelected)];
        
        button.tag = i;
        
        [self.titleButtons addObject:button];
        [self.titleScView addSubview:button];
        
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        if(i == 0){
            button.selected = YES;
            [self buttonAction:button];
        }
    }
}

//标题的点击事件
-(void)buttonAction:(UIButton *)sender{
    
    //设置选中的标题颜色
    [self setButton:sender];
    
    //计算滚动位置
    NSUInteger index = sender.tag;
    CGFloat offsetX = index * LeeWidth;
    self.pagesScVIew.contentOffset = CGPointMake(offsetX, 0);
    [self showVC:index];
//    [self upTitleCenter:sender];
    
}
//button 的选中状态
-(void)setButton:(UIButton *)button{
    
    self.selButton.selected = NO;
    self.selButton.transform = CGAffineTransformIdentity;
    button.selected = YES;
    button.transform = CGAffineTransformMakeScale(1.3, 1.3);
    self.selButton = button;
    
}


//标题字体的缩放
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat curPage = scrollView.contentOffset.x / scrollView.bounds.size.width;
    //    获取左边 button 和右边 button 的角标
    NSInteger leftIndex = curPage;
    NSInteger rightIndex = leftIndex + 1;
    
    UIButton *leftButton = self.titleButtons[leftIndex];
    UIButton *rightButton;
    if(rightIndex < self.titleButtons.count - 1){
        rightButton = self.titleButtons[rightIndex];
    }
    //缩放比例(0~1)
    CGFloat rightScale = curPage - leftIndex;
    CGFloat leftScale = 1 - rightScale;
    
    //缩放
    leftButton.transform = CGAffineTransformMakeScale(leftScale * 0.3 +1, leftScale * 0.3 +1);
    rightButton.transform = CGAffineTransformMakeScale(rightScale * 0.3 +1, rightScale * 0.3 +1);
    
    
}
//获取子控制器
-(void)showVC:(NSUInteger)index{
    
    UIViewController *VC = self.childViewControllers[index];
    if(VC.isViewLoaded)return;
    VC.view.frame = CGRectMake(index *LeeWidth, 0, LeeWidth, self.pagesScVIew.frame.size.height);
    [self.pagesScVIew addSubview:VC.view];
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSUInteger index = self.pagesScVIew.contentOffset.x / self.pagesScVIew.bounds.size.width;
    //获取对应的子控制器
    [self showVC:index];
    UIButton *selButton = self.titleButtons[index];
    selButton.selected = YES;
    [self setButton:selButton];
//    [self upTitleCenter:selButton];
    
    
}







@end
